package com.tutou.ikgroup.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tutou.ikgroup.pojo.po.Message;
import com.tutou.ikuser.pojo.vo.RedisUser;
import lombok.Data;

import java.util.*;

@Data
public class ChangeType {
    private static ObjectMapper objectMapper;

    public static <T> List<T> changeTypeStoL(String str, Class<T> type) {
        objectMapper = new ObjectMapper();
        List<T> list = null;
        try {
            list = objectMapper.readValue(str, new TypeReference<List<T>>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static <T> T changeTypeStoO(String strList, Class<T> type) {
        objectMapper = new ObjectMapper();
        T result = null;
        try {
            result = objectMapper.readValue(strList, type);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List<Message> changeTypeLtoM(List<String> msgList) {
        List<Message> messages = new ArrayList<>();
        for (String msg : msgList) {
            Message message = new Message();
            String[] parts = msg.split("：");
            message.setSenderName(parts[0]);
            message.setContent(parts[1]);
            messages.add(message);
        }
        return messages;
    }

    public static List<Message> changeTypeSetToL(Set<String> msg) {
        List<Message> msgs = new ArrayList<>();
        msg.forEach(s -> {
            Message message = new Message();
            message.setContent(s);
            msgs.add(message);
        });
        return msgs;
    }

}
